package com.cskaoyan.service.config;

import com.cskaoyan.bean.MallSystem;
import com.cskaoyan.bean.MallSystemExample;
import com.cskaoyan.mapper.MallSystemMapper;
import com.cskaoyan.model.vo.config.ExpressConfigVO;
import com.cskaoyan.model.vo.config.MallConfigVO;
import com.cskaoyan.model.vo.config.OrderConfigVO;
import com.cskaoyan.model.vo.config.WxConfigVO;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description: 配置管理
 * @author: shiyue
 * @create: 2021/11/30 10:53
 **/
@Service
public class SystemConfigServiceImpl implements SystemConfigService {
    @Autowired
    MallSystemMapper mallSystemMapper;

    @Override
    public MallConfigVO getMallConfig() {
    /*    MallSystemExample example = new MallSystemExample();
        MallSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameLike("mall");
        criteria.andIdEqualTo(1);
        List<MallSystem> list = mallSystemMapper.selectByExample(example);*/

        MallSystem mallSystem_longitude = mallSystemMapper.selectByPrimaryKey(13);
        MallSystem mallSystem_latitude = mallSystemMapper.selectByPrimaryKey(3);
        MallSystem mallSystem_address = mallSystemMapper.selectByPrimaryKey(16);
        MallSystem mallSystem_qq = mallSystemMapper.selectByPrimaryKey(9);
        MallSystem mallSystem_phone = mallSystemMapper.selectByPrimaryKey(14);
        MallSystem mallSystem_name = mallSystemMapper.selectByPrimaryKey(7);

        MallConfigVO configVO = new MallConfigVO();
        configVO.setMarket_mall_longitude(mallSystem_longitude.getKeyValue());
        configVO.setMarket_mall_latitude(mallSystem_latitude.getKeyValue());
        configVO.setMarket_mall_address(mallSystem_address.getKeyValue());
        configVO.setMarket_mall_qq(mallSystem_qq.getKeyValue());
        configVO.setMarket_mall_phone(mallSystem_phone.getKeyValue());
        configVO.setMarket_mall_name(mallSystem_name.getKeyValue());

        return configVO;
    }

    @Override
    public int updateMallConfig(MallConfigVO mallConfigVO) {
        MallSystem record1 = new MallSystem();
        record1.setId(16);
        record1.setUpdateTime(new Date());
        record1.setKeyValue(mallConfigVO.getMarket_mall_address());

        MallSystem record2 = new MallSystem();
        record2.setId(3);
        record2.setUpdateTime(new Date());
        record2.setKeyValue(mallConfigVO.getMarket_mall_latitude());

        MallSystem record3 = new MallSystem();
        record3.setId(13);
        record3.setUpdateTime(new Date());
        record3.setKeyValue(mallConfigVO.getMarket_mall_longitude());

        MallSystem record4 = new MallSystem();
        record4.setId(7);
        record4.setUpdateTime(new Date());
        record4.setKeyValue(mallConfigVO.getMarket_mall_name());

        MallSystem record5 = new MallSystem();
        record5.setId(14);
        record5.setUpdateTime(new Date());
        record5.setKeyValue(mallConfigVO.getMarket_mall_phone());

        MallSystem record6 = new MallSystem();
        record6.setId(9);
/*        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间*/
        record6.setUpdateTime(new Date());
        record6.setKeyValue(mallConfigVO.getMarket_mall_qq());


        mallSystemMapper.updateByPrimaryKey2(record1);
        mallSystemMapper.updateByPrimaryKey2(record2);
        mallSystemMapper.updateByPrimaryKey2(record3);
        mallSystemMapper.updateByPrimaryKey2(record4);
        mallSystemMapper.updateByPrimaryKey2(record5);
        mallSystemMapper.updateByPrimaryKey2(record6);
        return 6;
    }

    @Override
    public ExpressConfigVO getExpressConfig() {
        MallSystem express_freight_min = mallSystemMapper.selectByPrimaryKey(6);
        MallSystem express_freight_value = mallSystemMapper.selectByPrimaryKey(8);


        ExpressConfigVO configVO = new ExpressConfigVO();
        configVO.setMarket_express_freight_min(express_freight_min.getKeyValue());
        configVO.setMarket_express_freight_value(express_freight_value.getKeyValue());

        return configVO;
    }

    @Override
    public int updateExpressConfig(ExpressConfigVO configVO) {
        MallSystem record1 = new MallSystem();
        record1.setId(6);
        record1.setUpdateTime(new Date());
        record1.setKeyValue(configVO.getMarket_express_freight_min());

        MallSystem record2 = new MallSystem();
        record2.setId(8);
        record2.setUpdateTime(new Date());
        record2.setKeyValue(configVO.getMarket_express_freight_value());

        mallSystemMapper.updateByPrimaryKey2(record1);
        mallSystemMapper.updateByPrimaryKey2(record2);
        return 2;
    }

    @Override
    public OrderConfigVO getOrderConfig() {
        OrderConfigVO orderConfigVO = new OrderConfigVO();
        MallSystemExample example = new MallSystemExample();
        MallSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameLike("order");


        List<MallSystem> list = mallSystemMapper.selectByExample(example);
        //System.out.println(list.get(0).getKeyValue()+"\n"+list.get(1).getKeyValue());
        for (MallSystem mallSystem : list) {
            System.out.println(mallSystem.getKeyName());
            if (mallSystem.getKeyName().equals("market_order_unpaid")) {
                orderConfigVO.setMarket_order_unpaid(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_order_unconfirm")) {
                orderConfigVO.setMarket_order_unconfirm(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_order_comment")) {
                orderConfigVO.setMarket_order_comment(mallSystem.getKeyValue());
            }
        }
        return orderConfigVO;
    }

    @Override
    public int updateOrderConfig(OrderConfigVO configVO) {
        MallSystem record1 = new MallSystem();
        MallSystem record2 = new MallSystem();
        MallSystem record3 = new MallSystem();

        record1.setKeyName("market_order_comment");
        record1.setKeyValue(configVO.getMarket_order_comment());
        record1.setUpdateTime(new Date());

        record2.setKeyName("market_order_unconfirm");
        record2.setKeyValue(configVO.getMarket_order_unconfirm());
        record2.setUpdateTime(new Date());

        record3.setKeyName("market_order_unpaid");
        record3.setKeyValue(configVO.getMarket_order_unpaid());
        record3.setUpdateTime(new Date());

        List<MallSystem> list = Arrays.asList(record1, record2, record3);
        //System.out.println(list);


        int rows = 0;
        for (MallSystem mallSystem : list) {
            MallSystemExample example = new MallSystemExample();
            MallSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(mallSystem.getKeyName());
            int row = mallSystemMapper.updateByExampleSelective(mallSystem, example);
            rows += row;
        }

        return rows;
    }

    @Override
    public WxConfigVO getWxConfig() {
        WxConfigVO orderConfigVO = new WxConfigVO();
        MallSystemExample example = new MallSystemExample();
        MallSystemExample.Criteria criteria = example.createCriteria();
       // criteria.andKeyNameLike("wxhomeindex");

        List<MallSystem> list = mallSystemMapper.selectByExample(example);

        for (MallSystem mallSystem : list) {
            System.out.println(mallSystem.getKeyName());
            if (mallSystem.getKeyName().equals("market_wx_index_new")) {
                orderConfigVO.setMarket_wx_index_new(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_index_topic")) {
                orderConfigVO.setMarket_wx_index_topic(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_share")) {
                orderConfigVO.setMarket_wx_share(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_index_brand")) {
                orderConfigVO.setMarket_wx_index_brand(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_catlog_goods")) {
                orderConfigVO.setMarket_wx_catlog_goods(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_catlog_list")) {
                orderConfigVO.setMarket_wx_catlog_list(mallSystem.getKeyValue());
            } else if (mallSystem.getKeyName().equals("market_wx_index_hot")) {
                orderConfigVO.setMarket_wx_index_hot(mallSystem.getKeyValue());
            }
        }
        return orderConfigVO;
    }

    @Override
    public int updateWxConfig(WxConfigVO configVO) {
        MallSystem record1 = new MallSystem();
        MallSystem record2 = new MallSystem();
        MallSystem record3 = new MallSystem();
        MallSystem record4 = new MallSystem();
        MallSystem record5 = new MallSystem();
        MallSystem record6 = new MallSystem();
        MallSystem record7 = new MallSystem();

        record1.setKeyName("market_wx_index_new");
        record1.setKeyValue(configVO.getMarket_wx_index_new());
        record1.setUpdateTime(new Date());

        record2.setKeyName("market_wx_index_topic");
        record2.setKeyValue(configVO.getMarket_wx_index_topic());
        record2.setUpdateTime(new Date());

        record3.setKeyName("market_wx_share");
        record3.setKeyValue(configVO.getMarket_wx_share());
        record3.setUpdateTime(new Date());

        record4.setKeyName("market_wx_index_brand");
        record4.setKeyValue(configVO.getMarket_wx_index_brand());
        record4.setUpdateTime(new Date());

        record5.setKeyName("market_wx_catlog_goods");
        record5.setKeyValue(configVO.getMarket_wx_catlog_goods());
        record5.setUpdateTime(new Date());

        record6.setKeyName("market_wx_catlog_list");
        record6.setKeyValue(configVO.getMarket_wx_catlog_list());
        record6.setUpdateTime(new Date());

        record7.setKeyName("market_wx_index_hot");
        record7.setKeyValue(configVO.getMarket_wx_index_hot());
        record7.setUpdateTime(new Date());


        List<MallSystem> list = Arrays.asList(record1, record2, record3,record4,record5,record6,record7);

        int rows = 0;
        for (MallSystem mallSystem : list) {
            MallSystemExample example = new MallSystemExample();
            MallSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(mallSystem.getKeyName());
            int row = mallSystemMapper.updateByExampleSelective(mallSystem, example);
            rows += row;
        }

        return rows;



    }
}
